package com.example.tests;


import java.util.ArrayList;
import java.io.File;
import java.io.FileReader;

import static com.example.tests.GroupDataGenerator.generateRandomGroups;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Random;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;

import com.example.fw.ApplicationManager;

public class TestBase {
	
	public static ApplicationManager app;
	

	@BeforeTest
	public void setUp() throws Exception {
		String configFile = System.getProperty("configFile", "application.properties");
		Properties properties = new Properties();
		properties.load(new FileReader(new File(configFile)));
		app = new ApplicationManager(properties);
		
	}

	@AfterTest
	public void tearDown() throws Exception {
		app.stop();
		
	}

	@DataProvider
	public Iterator<Object[]> randomValidGroupGenerator(){
		return wrapGroupsForDataProvider(generateRandomGroups(5)).iterator();
	}
	
	

	public static List<Object[]> wrapGroupsForDataProvider(List<GroupData> groups) {
		List<Object[]> list = new ArrayList<Object[]>();
		for (GroupData group : groups) {
			list.add(new Object[]{group});
		}
		return list;
	}

	

	@DataProvider
	public Iterator<Object[]> randomValidContactGenerator() {
		List<Object[]> list = new ArrayList<Object[]>();
		for (int i = 0; i < 5; i++) {
		ContactData contact = new ContactData()
		.withFirstName(GenerateContactRandomString())  
		.withLastName(GenerateContactRandomString())  
		.withAdressFirst(GenerateContactRandomString())
		.withHomeTel(GenerateContactRandomString())   
		.withMobileTel(GenerateContactRandomString()) 
		.withWorkTel(GenerateContactRandomString())   
		.withEmailFirst(GenerateContactRandomString())
		.withEmailSec(GenerateContactRandomString())  
		.withBday(GenerateContactRandomDay())      
		.withBmonth(GenerateContactRandomMonth())    
		.withByear(GenerateContactRandomYear())     
		.withAdressSec(GenerateContactRandomString()) 
		.withHomeSec(GenerateContactRandomString()); 
		list.add(new Object[]{contact});
		}	
		return list.iterator();
	}
	
	public static List<Object[]> wrapContactsForDataProvider(List<ContactData> contacts) {
		List<Object[]> list = new ArrayList<Object[]>();
		for (ContactData contact : contacts) {
			list.add(new Object[]{contact});
		}
		return list;
	}



  
	public String GenerateContactRandomYear() {
		Random rnd = new Random();

        if (rnd.nextInt(3) == 0) {

                return "-";

        } else {

                return Integer.toString(rnd.nextInt(9998)+1);
        }
	}
        
	public String GenerateContactRandomDay() {
		Random rnd = new Random();

        if (rnd.nextInt(3) == 0) {

                return "-";

        } else {

                return Integer.toString(rnd.nextInt(30)+1) ;

        }

	}
	public int GenerateContactRandomMonth() {
		Random rnd = new Random();
		int monthIndex = rnd.nextInt(12);
        return monthIndex;
     }
	public String GenerateContactRandomString() {
		Random rnd = new Random();
		if (rnd.nextInt(5) == 0) {
			return ""; 	
		}else {
			return "test" + rnd.nextInt(); 
		}
		
	}
}
